package com.dfbz.bigdata.modules.system.controller;


import com.dfbz.bigdata.common.utils.R;
import com.dfbz.bigdata.modules.system.doman.DesignerCase;
import com.dfbz.bigdata.modules.system.dto.DesignerCaseDto;
import com.dfbz.bigdata.modules.system.service.DesignerCaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/case")
@Slf4j
public class CommonCaseController {


    @Autowired
    private DesignerCaseService designerCaseService;

    //    @GetMapping
//    public R<Page> getCaseList(String caseTitle, Long currentPage, Long pagesizes){
//        log.info("{}",caseTitle);
//        Page page =new Page(currentPage,pagesizes);
//
//        LambdaQueryWrapper<DesignerCase> lqw=new LambdaQueryWrapper<>();
//        lqw.like(!caseTitle.isEmpty(),DesignerCase::getCaseTitle,caseTitle);
//        designerCaseService.page(page, lqw);
//
//        return R.success(page);
//    }
//

//    小程序列表
    @GetMapping("/miniapp/list")
    public R<List<DesignerCaseDto>> getMiniCaseList(Integer parent, Integer style) {
        List<DesignerCaseDto> miniCaseList = designerCaseService.getMiniCaseList(parent, style);

        return R.success(miniCaseList);
    }

    @GetMapping("/miniapp")
    public R<DesignerCaseDto> getMiniCaseByid( Long id) {
        DesignerCaseDto caseByid = designerCaseService.getCaseListByid(id);

        return R.success(caseByid);
    }

    //    查看后台列表
    @GetMapping
    public R<List<DesignerCaseDto>> getCaseList(String caseTitle, int currentPage, int pagesizes) {
        List<DesignerCaseDto> caseList = designerCaseService.getCaseList(caseTitle, currentPage, pagesizes);

        for (DesignerCaseDto designerCase : caseList) {
            designerCase.setDesignerMsg("作者等级: " + designerCase.getDesignerLabel()
                    + "  作者:" + designerCase.getDesignerName());
        }

        return R.success(caseList);
    }

    @GetMapping("/total")
    public R<Integer> getDicTotal() {

        return R.success(designerCaseService.getTotal());

    }


    @PutMapping("update")
    public R<String> updateGoods(@RequestBody DesignerCase designerCase) {

        log.info("更新----------------{}", designerCase);
        boolean b = designerCaseService.updateById(designerCase);
        if (b) {
            return R.success("更新成功^_^!!!!!");
        }
        return R.error("服务器繁忙-_-!!");

    }


    @PostMapping("insert")
    public R<String> insert(@RequestBody DesignerCase designerCase) {
        log.info("新增----------------{}", designerCase);
        designerCase.setCreateTime(new Date());
        designerCase.setUpdateTime(new Date());
        boolean b = designerCaseService.save(designerCase);
        if (b) {
            return R.success("新增成功^_^!!!!!");
        }
        return R.success("服务器繁忙-_-!!");

//        return R.success("test");

    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id) {

        boolean b = designerCaseService.removeById(id);
        if (b) {
            return R.success("删除成功^_^!!!!!");
        } else return R.error("系统繁忙-_-!!");

    }
}
